﻿#include "FITKAbaqusAdaptorOutput.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITKAbaqusAdaptorOutputField.h"
#include "FITKAbaqusAdaptorOutputHistory.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaFieldOutput.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaHistoryOutput.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"

//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorOutput::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorOutput";
    }

    void FITKAbaqusAdaptorOutput::setCaseData(AbaqusData::FITKDataCase* caseData)
    {
        _caseData = caseData;
    }

    void FITKAbaqusAdaptorOutput::setStepData(Interface::FITKAbaAbstractStep * stepData)
    {
        _stepData = stepData;
    }

    bool FITKAbaqusAdaptorOutput::adaptR()
    {
        _outputData = dynamic_cast<Interface::FITKAbaAbstractOutput*>(_dataObj);
        if (!_outputData || !_reader || !_caseData || !_stepData) return false;
        //校验信息
        //QString line = _reader->previousLine();
        //if (!line.toLower().contains("output")) return false;
        //回退一行 处理关键字信息 
        _reader->backLine();
        bool isR = false;
        QString line = _reader->readLine();
        InpKeyLineSpliter s(line, _reader);
        ////互斥参数的最后一个 其余两个为field、history
        QString diagnostics = s["diagnostics"];
        ////共同的可选参数
        QString outputName = s["name"];
        ////Standard下的可选参数
        QString frequency = s["frequency"];
        QString modeList = s["mode list"];
        QString numberInterval = s["number interval"];
        QString timeMarks = s["time marks"];
        QString timePoints = s["time points"];
        QString sH = s.getHeader().toLower();
        //_outputData->setDataObjectName(outputName);
        if (_outputData->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::Field)
        {
            isR = readField();
        }
        else if (_outputData->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::History)
        {
            isR = readHistory();
            //isR = true;
        }
        else
        {
            _reader->consoleMessage(3, "Read Output Error Type Error");
        }

        if (!isR)
        {
            _reader->consoleMessage(3, line);

            _reader->consoleMessage(3, QString("Read Output Error ,%1 File:%2 Line:%3").arg(sH).arg(__FILE__).arg(__LINE__));
        }
        return isR;
    }

    bool FITKAbaqusAdaptorOutput::adaptW()
    {
        _outputData = dynamic_cast<Interface::FITKAbaAbstractOutput*>(_dataObj);
        if (!_outputData || !_writer || !_caseData) return false;
        bool isW = false;
        if (_outputData->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::Field)
        {
            isW = writeField();
        }
        else if (_outputData->getOutputType() == Interface::FITKAbaAbstractOutput::FITKAbaOutputType::History)
        {
            isW = writeHistory();
        }
        //写出
        return isW;
    }

    bool FITKAbaqusAdaptorOutput::readField()
    {
        auto field = dynamic_cast<Interface::FITKAbaFieldOutput*>(_dataObj);
        if (field == nullptr) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorOutputField>("INP", field);
        if (adaptor == nullptr) return false;
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setCaseData(_caseData);
        adaptor->setStepData(_stepData);
        adaptor->setDataObject(field);
        bool ok = adaptor->adaptR();
        return ok;
    }

    bool FITKAbaqusAdaptorOutput::readHistory()
    {
        auto history = dynamic_cast<Interface::FITKAbaHistoryOutput*>(_dataObj);
        if (history == nullptr) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorOutputHistory>("INP", history);
        if (adaptor == nullptr) return false;
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setCaseData(_caseData);
        adaptor->setStepData(_stepData);
        adaptor->setDataObject(history);
        bool ok = adaptor->adaptR();
        return ok;
    }

    bool FITKAbaqusAdaptorOutput::writeField()
    {
        auto field = dynamic_cast<Interface::FITKAbaFieldOutput*>(_dataObj);
        if (field == nullptr) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorOutputField>("INP", field);
        if (adaptor == nullptr) return false;
        //设置写出器
        adaptor->setFileWriter(_writer);
        adaptor->setCaseData(_caseData);
        adaptor->setDataObject(field);
        bool ok = adaptor->adaptW();
        if (!ok)
            _writer->consoleMessage(3, QString("Write Field Output Field Error, File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
        return ok;
    }

    bool FITKAbaqusAdaptorOutput::writeHistory()
    {
        auto history = dynamic_cast<Interface::FITKAbaHistoryOutput*>(_dataObj);
        if (history == nullptr) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorOutputHistory>("INP", history);
        if (adaptor == nullptr) return false;
        //设置写出器
        adaptor->setFileWriter(_writer);
        adaptor->setCaseData(_caseData);
        adaptor->setDataObject(history);
        bool ok = adaptor->adaptW();
        if (!ok)
            _writer->consoleMessage(3, QString("Write Field Output History Error, File:%1 Line:%2").arg(__FILE__).arg(__LINE__));
        return ok;
    }
}
